package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.server.mapper.QuestionMapper;

import com.tanhua.server.pojo.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: JinboYang
 * @Description:
 * @Date: Created in 下午 19:50 2021/1/13
 * @ModifiedBy:
 */
@Service
public class QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    public Question queryQuestion(Long userId) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userId);
        return this.questionMapper.selectOne(queryWrapper);
    }

    /**
       * @Description: 保存
       * @Param:[userId, content]
       * @return:void
       * @Author:JinboYang
       * @Date:2021/1/16
       */
    public void save(Long userId, String content) {
        Question question = this.queryQuestion(userId);
        if(question!=null){
            question.setTxt(content);
            this.questionMapper.updateById(question);
        }else {
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            this.questionMapper.insert(question);
        }
    }
}
